<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="pg" uri="/tags/pagination"%>
<%
	String path = request.getContextPath();

	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>代理用户管理</title>
		<s:include value="../common/include.jsp"></s:include>
		<meta http-equiv="pragma" content="no-cache">
		<script type="text/javascript">
	//查询
	function doQuery() {
		showBar();
		//initPage("agentForm");
		//chgAction("agent", "query");
		//$.post("agent!checkAgentId.action", {
		//	agentId : txt
		//}, function(obj) {
		//	$("#checkAgentIdText").html(obj.resultStr);
		//});
		var agentName = $("#agentName").val();
		$('#dg').datagrid(
				{
					url : 'agent!query.action?agentId=' + $("#agentId").val()
							+ '&agentName=' + $("#agentName").val()
				});
	}
	//新增
	function add() {
		vReturnValue = window
				.showModalDialog(
						"agent!toAdd.action",
						"",
						"center:no;dialogLeft:100px;dialogTop:100px;scroll:0;status:no;help:0;resizable:0;dialogWidth:520px;dialogHeight:400px");
		if ('0' == vReturnValue) {
			//$.messager.alert('提示讯息', '用户新增成功!', 'info');
			show('用户新增成功!');
			$('#dg').datagrid(
					{
						url : 'agent!query.action?agentId='
								+ $("#agentId").val() + '&agentName='
								+ $("#agentName").val()
					});
		}
	}
	//修改
	function modify() {
		var row = $('#dg').datagrid('getSelected');
		vReturnValue = window
				.showModalDialog(
						"agent!toModify.action?agentId=" + row.agent_id,
						window,
						"center:no;dialogLeft:100px;dialogTop:100px;scroll:0;status:no;help:0;resizable:0;dialogWidth:520px;dialogHeight:400px");
		if ('0' == vReturnValue) {
			//$.messager.alert('提示讯息', '用户修改成功!', 'info');
			show('用户修改成功!');
			$('#dg').datagrid(
					{
						url : 'agent!query.action?agentId='
								+ $("#agentId").val() + '&agentName='
								+ $("#agentName").val()
					});
		}

	}
	//删除
	function remove() {
		var ss = [];
		var rows = $('#dg').datagrid('getSelections');
		//var row = $('#dg').datagrid('getSelected');
		if (rows.length == 1) {
			//仅仅选中一笔资料
			var row = rows[0];
			$.messager.confirm('请再次确认', '是否删除用户ID=' + row.agent_id + '?',
					function(r) {
						if (r) {
							$.post("agent!delete.action", {
								agentId : row.agent_id
							}, function(obj) {
								//$.messager.alert('Info', obj.resultStr);
								show(obj.resultStr);
								$('#dg').datagrid(
										{
											url : 'agent!query.action?agentId='
													+ $("#agentId").val()
													+ '&agentName='
													+ $("#agentName").val()
										});
							});
						}
					});
		} else if (rows.length > 1) {
			$.messager.confirm('请再次确认', '是否批量删除选择的资料？', function(r) {
				if (r) {
					var ss = [];
					for ( var i = 0; i < rows.length; i++) {
						var row = rows[i];
						ss.push(row.agent_id);
					}

					$.ajax({
						type : "POST",
						url : "agent!delete.action",
						data : "agentId=" + ss,
						dataType : "json",
						success : function(request) {
							//document.location.reload();
							//$.messager.alert('Info', request.resultStr);
							show(request.resultStr);
							$('#dg').datagrid(
									{
										url : 'agent!query.action?agentId='
												+ $("#agentId").val()
												+ '&agentName='
												+ $("#agentName").val()
									});
						},
						error : function(request, error) {
							alert('Error deleting item(s), try again later.');
						}
					})
				}
			});

		}
	}
	//得到datagrid中选取的一笔资料
	function getSelected() {
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			$.messager.alert('Info', row.agent_id + ":" + row.agent_name);
		}
	}
	//得到datagrid中选取的多笔资料
	function getSelections() {
		var ss = [];
		var rows = $('#dg').datagrid('getSelections');
		for ( var i = 0; i < rows.length; i++) {
			var row = rows[i];
			ss.push('<span>' + row.agent_id + ":" + row.agent_name + '</span>');
		}
		$.messager.alert('Info', ss.join('<br/>'));
	}
	//初始化datagrid，并加载数据
	$(function() {
		$('#dg').datagrid(
				{
					iconCls : 'icon-ok',
					nowrap : false,
					//striped: true,
					//collapsible:true,
					url : 'agent!query.action?agentId=' + $("#agentId").val()
							+ '&agentName=' + $("#agentName").val(),
					loadMsg : '数据装载中......',
					remoteSort : false,
					pagination : true,
					rownumbers : true,
					toolbar : [ {
						text : '添加',
						iconCls : 'icon-add',
						handler : function() {
							//openDialog("add_dialog","add"); 
							add();
						}
					}, '-', {
						text : '修改',
						iconCls : 'icon-edit',
						handler : function() {
							modify();
						}
					}, '-', {
						text : '删除',
						iconCls : 'icon-remove',
						handler : function() {
							//delAppInfo(); 
							//getSelections();
							remove();
						}
					} ]
				});
	});
	function show(msg) {
		$.messager.show({
			title : '提示讯息',
			msg : msg,
			showType : 'show'
		});
	}
</script>
	</head>
	<body class="easyui-panel">
		<span class="outSpan"> <s:form name="agent">
				<div class="easyui-panel" title="查询条件" style="width: 800px;">
					<table>
						<tr>
							<td class="menu-text">
								AGENT_ID：
							</td>
							<td>
								<s:textfield cssClass="easyui-validatebox" id="agentId"
									maxlength="20" name="agentForm.agent_id"></s:textfield>
							</td>
							<td class="menu-text">
								用户姓名：
							</td>
							<td>
								<s:textfield id="agentName" name="agentForm.agent_name"
									maxlength="20"></s:textfield>
							</td>
							<td class="">
								<a href="javascript:void(0)" class="easyui-linkbutton"
									onclick="doQuery()" data-options="iconCls:'icon-search'">查询</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									onclick="add();" data-options="iconCls:'icon-add'">新增</a>
							</td>
						</tr>
					</table>
				</div>
				<div id="abc" style="margin: 10px 0;"></div>
				<table id="dg" title="查询结果" style="width: 800px; height: 350px">
					<thead>
						<tr>
							<th data-options="field:'ck',checkbox:true"></th>
							<th data-options="field:'agent_id',width:200">
								用户ID
							</th>
							<th data-options="field:'agent_name',width:200">
								用户姓名
							</th>
							<th data-options="field:'depart_name',width:200">
								部门
							</th>
							<th data-options="field:'agent_flagDsp',width:100">
								状态
							</th>
						</tr>
					</thead>
				</table>
			</s:form> </span>
	</body>
</html>
